package com.akcome.apollo.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.akcome.apollo.domain.VBmpAcount;
import com.akcome.apollo.repository.VBmpAcountRepository;
import com.akcome.apollo.service.VBmpAcountService;


/**
 * VIEWService接口实现
 *
 * @author wn
 * @version $v: 1.0.0, $time:2017-12-26 08:49:00 Exp $
 */
@Service
public class VBmpAcountServiceImpl implements VBmpAcountService {

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private VBmpAcountRepository vBmpAcountRepository;
	
	@Override
	public VBmpAcount save(VBmpAcount vBmpAcount){
		return vBmpAcountRepository.save(vBmpAcount);
	}
	
	@Override
	public VBmpAcount findOne(Long id){
		return vBmpAcountRepository.findOne(id);
	}
	
	@Override
	public Page<VBmpAcount> findByPage(Long updataTime,Pageable pageable){
		return vBmpAcountRepository.findAll(new Specification<VBmpAcount>() {
			
			@Override
			public Predicate toPredicate(Root<VBmpAcount> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
				List<Predicate> params = new ArrayList<Predicate>();
                if (null != updataTime) {
                    Path<Date> updateTimePath = root.get("updateTime");
                    params.add(criteriaBuilder.greaterThanOrEqualTo(updateTimePath, new Date(updataTime)));
                }
                criteriaQuery.orderBy(criteriaBuilder.desc(root.get("id")));
                Predicate[] predicates = new Predicate[params.size()];
                criteriaQuery.where(params.toArray(predicates));
				return null;
			}
		}, pageable);
	}
	
	@Override
	public List<VBmpAcount> getUserInfoList(Long update_time,String coinTypeList,int pageIndex,int size){
		String[] coinTypes = coinTypeList.split(",");
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if(pageIndex==0){
			return vBmpAcountRepository.getUserInfoList(sf.format(new Date(update_time)) , Arrays.asList(coinTypes));
		}
		return vBmpAcountRepository.getUserInfoList(sf.format(new Date(update_time)), Arrays.asList(coinTypes), (pageIndex-1)*size, pageIndex*size-1);
	}
}
